package com.tadpole.music.view.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.home.NewInfoListAdapter;

import com.tadpole.music.bean.PushBean;
import com.tadpole.music.bean.home.NewInfoListBean;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.home.NewInfoListIView;
import com.tadpole.music.presenter.home.NewInfoListPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.web.H5Activity;
import com.tadpole.music.view.activity.base.BaseActivity;

import java.util.List;

/**
 * 通知公告列表
 * Created By 孙岩
 * on 2020/12/5
 */
public class NewInfoListActivity extends BaseActivity implements NewInfoListIView {
    //回退
    private View view_back_icon;
    //标题
    private TextView actionbar_title;
    //刷新 加载
    private SmartRefreshLayout rl_refresh;
    //recyclerView
    private RecyclerView recyclerView;
    //adapter
    private NewInfoListAdapter newInfoListAdapter;
    //presenter
    private NewInfoListPresenter newInfoListPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_info);
        initViews();
        initListeners();

        newInfoListPresenter = new NewInfoListPresenter();
        newInfoListPresenter.attachView(this);
        newInfoListPresenter.getNewInfoList(false);
    }

    //初始化控件
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        actionbar_title = findViewById(R.id.actionbar_title);
        actionbar_title.setText("通知公告");
        rl_refresh = findViewById(R.id.rl_refresh);
        recyclerView = findViewById(R.id.recyclerView);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                NewInfoListActivity.this.finish();
            }
        });

        //添加刷新监听器
        rl_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@io.reactivex.annotations.NonNull RefreshLayout refreshLayout) {
                newInfoListPresenter.getNewInfoList(true);
            }
        });

        //上拉加载
        rl_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@androidx.annotation.NonNull RefreshLayout refreshLayout) {
                newInfoListPresenter.getNewInfoListMore();
            }
        });
    }

    @Override
    public void showNewInfoList(final List<NewInfoListBean.DataBeanX.DataBean> list) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(NewInfoListActivity.this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        newInfoListAdapter = new NewInfoListAdapter(NewInfoListActivity.this,list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(newInfoListAdapter);
        newInfoListAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if(Mutils.isNetworkAvailable()){
                    PushBean pushBean = new PushBean();
                    pushBean.setUrl(UrlConfig.getNoticeInfo+list.get(position).getId());
                    pushBean.setTitle("公告详情");
                    Intent intent = new Intent(NewInfoListActivity.this, H5Activity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("params",pushBean);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void notifyAdapter() {
        newInfoListAdapter.notifyDataSetChanged();
    }

    @Override
    public void stopRefresh() {
        rl_refresh.finishRefresh();
        rl_refresh.finishLoadMore();
    }
}
